var MapMayhem = {
	gmap: null,
	ggeocoder: null,
	markers: [],
	
	init: function(inst_id)
	{
		this.initMap(inst_id);
	},
	
	mapAvailable: function()
	{
		if (this.gmap && this.ggeocoder)
			return true;
	},
	
	initMap: function(inst_id)
	{
		if (!GBrowserIsCompatible() || this.mapAvailable())
			return false;
		
		var gmap 	 = new GMap2(document.getElementById('map_'+inst_id));
		var geocoder = new GClientGeocoder();
		
		if (!geocoder || !gmap)
			return false;
		
		gmap.addControl(new GLargeMapControl());
		this.gmap 		= gmap;
		this.ggeocoder 	= geocoder;
		
		this.gmap.setCenter(new GLatLng(0, 0), 3);
		
		return true;
	},
	
	addMarker: function(data, content_id, content_title, lat, lon, center_map)
	{
		var point = new GLatLng(lat, lon);
		
		if (center_map)
			this.gmap.setCenter(point, 17);
		
		var marker = new GMarker(point);
		this.gmap.addOverlay(marker);
		
		// listen for clicks on the marker
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml('<div class="map_bubble"><h3><a href="/content/'+content_id+'">'+content_title+'</a></h3><p>'+data+'</p></div>');
		});
		
		return marker;
	},
	
	addOverlayMarker: function(key, url, title, desc, lat, lon, provider, provider_url, provider_icon)
	{
		//var point = new GLatLng(lat, lon);
		//
		//// custom icon goodness
		//var overlayIcon 	= new GIcon(G_DEFAULT_ICON);
		//overlayIcon.image 	= provider_icon;
		//overlayIcon.shadow	= "";
		//marker_opts 		= { icon:overlayIcon };
		//
		//var marker = new GMarker(point, marker_opts);
		//this.gmap.addOverlay(marker);
		//
		//// listen for clicks on the marker
		//GEvent.addListener(marker, "click", function() {
		//	marker.openInfoWindowHtml("<a href=\""+url+"\">"+title+"</a><br />"+desc+"<br /><br /><em>Provided by <a href=\""+provider_url+"\">"+provider+"</a></em>");
		//});
		//
		//return marker;
	},
	
	centerMapWithLowestZoom: function(max_lat, max_lng, min_lat, min_lng)
	{
		if (this.markers.length == 0)
			return false;
			
		var bounds = new GLatLngBounds(new GLatLng(max_lat, max_lng), new GLatLng(min_lat, min_lng)); // NE to SW
		
		this.gmap.setZoom(this.gmap.getBoundsZoomLevel(bounds));
		this.gmap.setCenter(bounds.getCenter());
	}
}